package com.xpsoft.oa.action.system;


import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.xpsoft.core.command.QueryFilter;
import com.xpsoft.core.util.BeanUtil;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.core.web.action.BaseAction;


import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.model.system.UserFlowconfig;
import com.xpsoft.oa.service.system.UserFlowconfigService;

import flexjson.JSONSerializer;
/**
 * 
 * @author 
 *
 */
public class UserFlowconfigAction extends BaseAction{
	@Resource
	private UserFlowconfigService userFlowconfigService;
	private UserFlowconfig userFlowconfig;
	
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public UserFlowconfig getUserFlowconfig() {
		return userFlowconfig;
	}

	public void setUserFlowconfig(UserFlowconfig userFlowconfig) {
		this.userFlowconfig = userFlowconfig;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<UserFlowconfig> list= userFlowconfigService.getAll(filter);
		
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		JSONSerializer serializer=JsonUtil.getJSONSerializer("");
		buff.append(serializer.exclude(new String[]{"class"}).serialize(list));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	/**
	 * 批量删除
	 * @return
	 */
	public String multiDel(){
		
		String[]ids=getRequest().getParameterValues("ids");
		if(ids!=null){
			for(String id:ids){
				userFlowconfigService.remove(new Long(id));
			}
		}
		
		jsonString="{success:true}";
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		UserFlowconfig userFlowconfig=userFlowconfigService.get(id);
		
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		JSONSerializer serializer=JsonUtil.getJSONSerializer("");
		sb.append(serializer.exclude(new String[]{"class"}).serialize(userFlowconfig));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 更新操作
	 */
	public String update(){
		
		userFlowconfigService.save(userFlowconfig);
		
		setJsonString("{success:true}");
		return SUCCESS;
	}
	
	/**
	 * 添加操作
	 */
	public String save(){
		String strUserId=getRequest().getParameter("userIds");
		String[] userIds=strUserId.split(",");
		for (int i = 0; i < userIds.length; i++) {
			UserFlowconfig temp=userFlowconfigService.getByUserId(Long.parseLong(userIds[i]));
			
			if(temp==null){
				temp=new UserFlowconfig();
			}
			AppUser user=new AppUser(Long.parseLong(userIds[i]));
			try {
				BeanUtil.copyNotNullProperties(temp, userFlowconfig);
			} catch (Exception e) {					
				logger.debug(e.getMessage());
				setJsonString("{success:false}");
				return SUCCESS;
			} 
			temp.setAppUser(user);
			userFlowconfigService.save(temp);
		}
		
		setJsonString("{success:true}");
		return SUCCESS;
	}
}
